package pathways;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;

import pathways.Database.Implementation.CDatabase;
import pathways.util.Debug.CDebug;

public class CEnzyme extends CCompound
              implements IListenableItem
{
  public CEnzyme(String name, long ID)
  {
    super(name,ID);
    p1 = new Point(0,0);
    p2 = new Point(0,0);
    connectingTo = 0;
    mouselistener = null;
    painter = null;
    visible = true;
  }

  public void setConnectingTo(int connectingTo)
  {
    // set the point the enzyme connects to substrate
    // Required by Enzyme Painter to draw the arrow head
    this.connectingTo = connectingTo;
  }

  public int getConnectingTo()
  {
    return connectingTo;
  }

  public void eventOccurred(String eventName, MouseEvent e)
  {
    if (eventName.equals("mouseClicked"))
    {
        if (mouselistener != null)
          mouselistener.mouseClicked(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
    else if (eventName.equals("mouseEntered"))
    {
        if (mouselistener != null)
          mouselistener.mouseEntered(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
    else if (eventName.equals("mouseExited"))
    {
        if (mouselistener != null)
          mouselistener.mouseExited(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
    else if (eventName.equals("mousePressed"))
    {
        if (mouselistener != null)
          mouselistener.mousePressed(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
    else if (eventName.equals("mouseReleased"))
    {
        if (mouselistener != null)
          mouselistener.mouseReleased(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
    else if (eventName.equals("mouseDragged"))
    {
        if (mouselistener != null)
          mouselistener.mouseDragged(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
    else if (eventName.equals("mouseMoved"))
    {
        if (mouselistener != null)
          mouselistener.mouseMoved(new CEnzymeMouseEvent(this,e,eventName));
        else
          return;
    }
  }

  public void setPoint1(Point p)
  {
    p1 = p;
  }

  public void setPoint2(Point p)
  {
    p2 = p;
  }

  public Point getPoint1()
  {
    return p1;
  }

  public Point getPoint2()
  {
    return p2;
  }

  public void draw(Graphics g, IEnzymePainter painter)
  {
    painter.draw(g,this);
  }

  public void addMouseListener(IEnzymeMouseListener listener)
  {
    mouselistener = listener;
  }

  public Shape getShape()
  {
    if (painter == null)
    {
      // raise exception
    }

    return painter.getEnclosingShape(this);
  }

  public IEnzymePainter getPainter()
  {
    return painter;
  }

  public void setPainter(IEnzymePainter painter)
  {
    this.painter = painter;
  }

  public boolean isVisible()
  {
    return visible;
  }

  public void setVisible(boolean visibility)
  {
    visible = visibility;
  }
  
  public String getDescription(CDatabase db)
  {
    String fullDescription = "";
    
    try {
    if (!this.ECNO.equals(""))
    {
      String part1 = ECNO.substring(0,1);
      String part2 = ECNO.substring(0,3);
      String part3 = ECNO.substring(0,5);
      
      String description1 = db.getSelect().getEcNumPartDescription(part1);
      String description2 = db.getSelect().getEcNumPartDescription(part2);
      String description3 = db.getSelect().getEcNumPartDescription(part3);
      
      fullDescription = description1 + " " + description2 + " " + description3;
    }
    } catch (Exception e) {
      CDebug.println(this,"Error: Invalid ECNO: " + ECNO);
      
      JOptionPane.showMessageDialog(null,"Error: Invalid ECNO: " + ECNO,"Error",JOptionPane.ERROR_MESSAGE); 
    }
    
    return fullDescription;
  }
  
  protected Point p1; // from
  protected Point p2; // to
  protected IEnzymeMouseListener mouselistener;
  protected int connectingTo;
  protected IEnzymePainter painter;
  protected boolean visible;
}